package com.ruiyangwang.ListenBook.servlet.album;

import com.ruiyangwang.ListenBook.dataObject.AlbumDO;
import com.ruiyangwang.ListenBook.repository.AlbumRepo;
import com.ruiyangwang.ListenBook.util.Log;
import com.ruiyangwang.ListenBook.viewObject.UserV0;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author syzygy
 * @date 2022/09/19 21:57
 **/
@WebServlet("/studio/album/unbind.do")
public class UnBindDoServlet extends HttpServlet {
    private final AlbumRepo albumRepo = new AlbumRepo();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int aid = Integer.parseInt(req.getParameter("aid"));
        int tid = Integer.parseInt(req.getParameter("tid"));
        HttpSession session = req.getSession(false);
        if(session != null){
            UserV0 user = (UserV0) session.getAttribute("currentUser");
            if(user != null) {
                AlbumDO albumDO = albumRepo.selectOneByAid(aid);
                if (albumDO == null) {
                    Log.println("要下线的专辑不存在：aid = " + aid);
                    resp.sendRedirect("/studio/album/list.html");
                    return;
                }
                if (!albumDO.uid.equals(user.uid)) {
                    Log.println("要下线的专辑不属于这个用户");
                    resp.sendRedirect("/studio/album/list.html");
                    return;
                }
                albumRepo.deleteByAidAndTid(aid, tid);
                resp.sendRedirect("/studio/album/relate.html?aid=" + aid);
            }
        }
        Log.println("用户未登录");
    }
}